#include "net_context.hpp"
#include <iostream>
#include <cstring>

using namespace event_framework;

class TcpClient
{
public:
    TcpClient(EventLoop& loop, const EndPoint& ep) : netCtx(loop, ep, TransProtocol::PROTO_TCP) {}
    ~TcpClient() = default;
    void Start()
    {
        netCtx.Init();
        auto& service = netCtx.GetTcpService();
        service.SetReadableCallback(std::bind(&TcpClient::OnMessage, this, std::placeholders::_1));
        service.SetWriteableCallback(std::bind(&TcpClient::Send, this, std::placeholders::_1));
        service.Connect();
    }
    void Stop()
    {
        service.Fini();
    }
private:
    void OnMessage(const Socket& socket)
    {
        char buf[64];
        bzero(buf, sizeof buf);
        socket.Recv(buf, sizeof buf);
        std::cout << "get result: " << buf << std::endl;
    }
    void Send(const Socket& socket)
    {
        char buf[] = "request";
        socket.Send(buf, sizeof buf);
    }
    NetContext netCtx;
};

int main()
{
    EventLoop loop;
    TcpClient client(loop, EndPoint("127.0.0.1", 6060));
    client.Start();
    std::cin.get();
    return 0;
}